$(function(){
	featured_tv_play.run();
});

var featured_tv_play = {
	run: function(){
		this._init_();			
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){
		this.showId = $("#showId").val() || "";
		var loc = location.href;
		this.category = loc.substring(loc.indexOf("category") + 9);
	},
	
	_start_: function(){
		var ua = navigator.userAgent;
		if(ua.indexOf("IE") > 0 ){
			$(".ie_player").show();
		}else{
			$(".ff_player").show();
		}
		
		this._highlight_nav();
		
		 main._play_close_side();
		
		//this._get_relate_anime();
	},
	
	_bind_events: function(){
		$("#forward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		$("#backward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		$("#displayEpisode").live("click", function() {
			$("#episodeDetail").toggle();
		});
		
		var locaIndex = location.href.indexOf("#"); 
		var localUrl = location.href;
		if(locaIndex > 0){
			localUrl = localUrl.substring(0,locaIndex);
		}
		$("#fb-comments").attr("data-href", localUrl);
	},
	
	_get_relate_anime: function(){
		var url = main.contextPath + "/world/featured/tv/relate/";
		var data = "category=" + this.category + "&showId=" + this.showId;
		$.ajax({
			url: url,
			cache: false,
			data: data,
			beforeSend: function(){
				$(".loadingBox").show();
			},
			success: function(result){
				$("#other_video").html(result.trim());
				featured_tv_play._handle_relate_tv_click();
				$(".loadingBox").hide();
			}
		});
	},
	
	_highlight_nav: function(){
		var menu = $("#side_menu > ul > li");
		if(this.category == "chinese"){
			menu.eq(1).addClass("curr");
		}else if(this.category == "japanese"){
			menu.eq(2).addClass("curr");
		}else if(this.category == "korea"){
			menu.eq(3).addClass("curr");
		}else if(this.category == "surprise"){
			menu.eq(4).addClass("curr");
		}else{
			menu.eq(0).addClass("curr");
		}
	},
	
	_handle_relate_tv_click: function(){
		$("#view_other > div.m_cont > div.m_box > div.img > a").click(function(){
			var href = $(this).attr("href");
			location.href = href + "#category=" + featured_tv_play.category;
			return false;
		});
		
		$("#view_other > div.m_cont > div.m_box > ul > li.name > a").click(function(){
			var href = $(this).attr("href");
			location.href = href + "#category=" + featured_tv_play.category;
			return false;
		});
	}
};